//
//  HR_Touch_ImageViewController.m
//  3DTouch
//
//  Created by duanhuifen on 2017/7/22.
//  Copyright © 2017年 huifen. All rights reserved.
//

#import "HR_Touch_ImageViewController.h"

@interface HR_Touch_ImageViewController ()

@property (strong, nonatomic)  UIImageView *picImageView;

@property (nonatomic,strong) UIImage *image;
//视图大小
@property (nonatomic,assign) CGSize showSize;

@end

@implementation HR_Touch_ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.picImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.picImageView];
    
    if (self.picName.length) {
        [self loadLocalImage:self.picName];
    }
    
    if (self.picUrlName.length) {
//        加载网络图片
    }
}


// 本地图片
- (void)loadLocalImage:(NSString *)imgName {
    self.image = [UIImage imageNamed:imgName];
    self.picImageView.image = self.image;
//    [self setImageViewWithImg:self.image];
}
//// 网络图片
//- (void)loadImgWithUrl:(NSString *)url {
//    __weak typeof(self) weakSelf = self;
//    [self.imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        [weakSelf setImageViewWithImg:image];
//    }];
//}

- (void)setImageViewWithImg:(UIImage *)img {
    CGSize showSize = [self newSizeByoriginalSize:img.size maxSize:self.view.bounds.size];
    self.picImageView.frame = CGRectMake(0, 0, showSize.width, showSize.height);
    self.showSize = showSize;
    self.preferredContentSize = showSize;
    [self.view sizeThatFits:self.showSize];
}

//获取图片和显示视图宽度的比例系数
- (float)getImgWidthFactor {
    return   self.view.bounds.size.width / self.image.size.width;
}
//获取图片和显示视图高度的比例系数
- (float)getImgHeightFactor {
    return  self.view.bounds.size.height / self.image.size.height;
}
//获取尺寸
- (CGSize)newSizeByoriginalSize:(CGSize)oldSize maxSize:(CGSize)mSize
{
    if (oldSize.width <= 0 || oldSize.height <= 0) {
        return CGSizeZero;
    }
    
    CGSize newSize = CGSizeZero;
    if (oldSize.width > mSize.width || oldSize.height > mSize.height) {
        //按比例计算尺寸
        float bs = [self getImgWidthFactor];
        float newHeight = oldSize.height * bs;
        newSize = CGSizeMake(mSize.width, newHeight);
        
        if (newHeight > mSize.height) {
            bs = [self getImgHeightFactor];
            float newWidth = oldSize.width * bs;
            newSize = CGSizeMake(newWidth, mSize.height);
        }
    }else {
        
        newSize = oldSize;
    }
    return newSize;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
